package com.itheima.homework15;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;

public class Test12 {
    public static void main(String[] args) throws Exception {
        ServerSocket ss=new ServerSocket(10089);
        new Thread(()->{
            BufferedOutputStream bos=null;
            while (true){
                try {
                    Socket socket = ss.accept();
                    //回应服务器端
                    BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                    //获得网络的文件
                    BufferedInputStream bis=new BufferedInputStream(socket.getInputStream());
                    //写到本地
                    String name= UUID.randomUUID().toString().replaceAll("-","");
                    bos=new BufferedOutputStream(new FileOutputStream("javaSEday15\\"+name+".png"));
                    int b;
                    while ((b=bis.read())!=-1){
                        bos.write(b);
                    }


                    bw.write("上传成功");
                    bw.newLine();
                    bw.flush();

                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                //  ss.close();
            }
        }).start();




    }
}
